from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from try_mysql import *
from PyQt5.uic import loadUiType
import cinema
from movie_record_manage_ui import Ui_Movie_record
import sys
ui, _ = loadUiType('movie_record_manage.ui')

class Movie_record_manage_App(QMainWindow, ui):
    #定义构造
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        #self.handle_ui_change()
        self.handle_buttons()
        self.show()
        self.show_tab()

    def handle_buttons(self):
        self.comfirm_add_rrecord.clicked.connect(self.Confirm_add)
        self.drop_record.clicked.connect(self.Drop_record)
        self.update_record.clicked.connect(self.Update_record)
        self.query_record.clicked.connect(self.Query_record)
        self.cancel_record_movie.clicked.connect(self.Close)


    #1.添加类别
    def Confirm_add(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieNum=self.movie_num.text()
        MovieName = self.add_movie_name.text()
        TimeRecord = self.add_time_record.text()
        SessionRcord = self.add_sessions_record.text()
        MovieGrade = self.add_movie_grade.text()
        FilmHouse = self.film_house.currentText()
        Population = self.population_movie.text()
        if (MovieNum=="" or MovieName== "" or TimeRecord=="" or SessionRcord == "" or MovieGrade == "" or FilmHouse == "" or Population == ""):
            QMessageBox.information(self, "提示", "主要字段不得为空", QMessageBox.Yes, QMessageBox.Yes)
            return
        else:
            warning = QMessageBox.warning(self, "提示", "你确定添加吗?", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                sql = "insert into movie_record_manage(MovieNum,MovieName,Timerecord,SessionRcord,MovieGrade,FilmHouse,Population) VALUES (%s,%s,%s,%s,%s,%s,%s)"
                values=(MovieNum,MovieName,TimeRecord,SessionRcord,MovieGrade,FilmHouse,Population)
                cur.execute(sql, values)
                conn.commit()
                close_conn(conn, cur)
                QMessageBox.information(self, "提示", "添加成功!", QMessageBox.Yes, QMessageBox.Yes)
                self.show_tab()
                self.clearEdit()
            return



    def show_tab(self):
        conn=get_conn()
        cur=conn.cursor()
        sql="select * from movie_record_manage order by MovieGrade asc"
        cur.execute(sql)
        data = cur.fetchall()
        if data:
            self.view_record_tab.setRowCount(0)
            self.view_record_tab.insertRow(0)
            for row , form in enumerate(data):
                for column ,item in enumerate(form):
                    self.view_record_tab.setItem(row,column,QTableWidgetItem(str(item)))
                    column += 1
                row_position=self.view_record_tab.rowCount()
                self.view_record_tab.insertRow(row_position)



    def Drop_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieNum=self.movie_num.text()
        MovieName = self.add_movie_name.text()

        if (MovieNum=="" or MovieName== ""):
            QMessageBox.information(self, "提示", "主要字段不得为空", QMessageBox.Yes, QMessageBox.Yes)
            return
        else:
            warning = QMessageBox.warning(self, "提示", "你确定删除吗?", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                sql1 = "delete from movie_record_manage where MovieNum=%s and MovieName=%s"
                cur.execute(sql1, (MovieNum, MovieName))
                conn.commit()
                close_conn(conn, cur)
                print(QMessageBox.information(self, "提醒", "删除成功", QMessageBox.Yes, QMessageBox.Yes))
                self.show_tab()
                self.clearEdit()
        return


    def Update_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieNum=self.movie_num.text()
        MovieName = self.add_movie_name.text()
        TimeRecord = self.add_time_record.text()
        SessionRcord = self.add_sessions_record.text()
        MovieGrade = self.add_movie_grade.text()
        FilmHouse = self.film_house.currentText()
        Population = self.population_movie.text()
        if (MovieNum=="" or MovieName == "" or TimeRecord=="" or SessionRcord=="" or MovieGrade=="" or FilmHouse=="" or Population==""):
            QMessageBox.information(self, "提示", "请明确更新条件", QMessageBox.Yes, QMessageBox.Yes)
            return
        else:
            warning = QMessageBox.warning(self, "提示", "你确定更新吗?", QMessageBox.Yes | QMessageBox.No)
            if warning == QMessageBox.Yes:
                sql1 = "update movie_record_manage set MovieName=%s,TimeRecord=%s, SessionRcord=%s ,MovieGrade=%s ,FilmHouse=%s ,Population=%s where MovieNum=%s"
                values = (MovieName, TimeRecord, SessionRcord, MovieGrade, FilmHouse, Population,MovieNum)
                cur.execute(sql1, values)
                conn.commit()
                close_conn(conn, cur)
                print(QMessageBox.information(self, "提醒", "更新成功", QMessageBox.Yes, QMessageBox.Yes))
                self.show_tab()
                self.clearEdit()
            return

    def Query_record(self):
        conn = get_conn()
        cur = conn.cursor()
        MovieNum=self.movie_num.text()
        MovieName = self.add_movie_name.text()
        sql2 = "select * from movie_record_manage where MovieNum=%s or MovieName=%s"
        cur.execute(sql2, (MovieNum, MovieName))
        data = cur.fetchall()
        if data:
            self.view_record_tab.setRowCount(0)
            self.view_record_tab.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.view_record_tab.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1
                row_position = self.view_record_tab.rowCount()
                self.view_record_tab.insertRow(row_position)
                self.statusBar().showMessage('查询结果如下')
        else:
            print(QMessageBox.information(self, "提醒", "查询无记录", QMessageBox.Yes, QMessageBox.Yes))

    def Close(self):
        self.Mainwindow=cinema.Cinema_App()
        self.close()
        self.Mainwindow.show()



    def clearEdit(self):
        self.movie_num.clear()
        self.add_movie_name.clear()
        self.add_time_record.clear()
        self.add_sessions_record.clear()
        self.add_movie_grade.clear()
        self.film_house.clear()
        self.population_movie.clear()

def main():
    app = QApplication([])
    window = Movie_record_manage_App()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()